//
//  AppDelegate.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/4.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseTabbarController.h"
#import "BaseNavigationController.h"
#import "LoginViewController.h"
#import "JJException.h"
#import "LaunchImageViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    BOOL ddd = [[DVUserManager shareManager] loadCacheUserInfo];
#if DEBUG //DEBUG环境
#else//正式
    // 开启防崩溃
    [JJException configExceptionCategory:JJExceptionGuardAll];
    [JJException startGuardException];
#endif
//    [NSThread sleepForTimeInterval:600];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"ISFIRSTLAUNCH"]) {
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:[[LaunchImageViewController alloc]init]];
        self.window.rootViewController = nav;
    } else {
        BaseTabbarController *tabVC = [[BaseTabbarController alloc] init];
        self.window.rootViewController = tabVC;
    }
    [self.window makeKeyAndVisible];
    // 一次性代码
    [self projectOnceCode];
    
    // 开启网络监听
    [[HWNetworkReachabilityManager shareManager] monitorNetworkStatus];
    
    // 初始化下载单例，若之前程序杀死时有正在下的任务，会自动恢复下载
    [HWDownloadManager shareManager];
    return YES;
}

/// 在这里写支持的旋转方向，为了防止横屏方向，应用启动时候界面变为横屏模式
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    // 可以这么写
    if (self.allowOrentitaionRotation) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    return UIInterfaceOrientationMaskPortrait;
}

// 应用处于后台，所有下载任务完成调用
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
{
    _backgroundSessionCompletionHandler = completionHandler;
}

// 一次性代码
- (void)projectOnceCode
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *onceKey = @"HWProjectOnceKey";
    if (![defaults boolForKey:onceKey]) {
        // 初始化下载最大并发数为1，不允许蜂窝网络下载
        [defaults setInteger:1 forKey:HWDownloadMaxConcurrentCountKey];
        [defaults setBool:YES forKey:HWDownloadAllowsCellularAccessKey];
        [defaults setBool:YES forKey:onceKey];
    }
}

@end
